package com.hbnu.guessanddraw;

import javax.websocket.*;
import java.net.URI;


@ClientEndpoint
public class WebSocketTestClient {

    private static Session session;

    @OnOpen
    public void onOpen(Session session) {
        WebSocketTestClient.session = session;
        System.out.println("连接成功");
    }

    @OnMessage
    public void onMessage(String message) {
        System.out.println("收到消息: " + message);
    }

    @OnError
    public void onError(Session session, Throwable throwable) {
        throwable.printStackTrace();
    }

    @OnClose
    public void onClose() {
        System.out.println("连接关闭");
    }

    public static void main(String[] args) throws Exception {
        WebSocketContainer container = ContainerProvider.getWebSocketContainer();
        URI uri = new URI("ws://localhost:8080/ws/room");
        container.connectToServer(WebSocketTestClient.class, uri);

        // 等待连接建立
        Thread.sleep(1000);

        // 发送测试消息
        session.getBasicRemote().sendText("{\"type\":\"createRoom\",\"userId\":\"u001\",\"roomName\":\"测试房间\"}");

        // 保持程序运行一段时间观察
        Thread.sleep(5000);
    }
}
